package com.dxt.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dxt.common.AppConstant;
import com.dxt.common.LogHelper;
import com.dxt.common.MyBusiException;
import com.dxt.message.AppRequestMessage;
import com.dxt.message.ReponseMessage;
import com.dxt.netty.RuiKongYdServerHandler;
import com.dxt.netty.utils.ReqStrUtils;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service("HYMufflerControlService")
public class HYMufflerControlService extends IBaseBusiService {
    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());

    @Autowired
    CacheManager cacheManager;

    @Override
    public ReponseMessage execute(String reqInfo, String url) {
        logger.debug(LogHelper._FUNC_START_());
        ReponseMessage message = new ReponseMessage();
        AppRequestMessage msg = getMsg();
        JSONObject jsonObject = JSON.parseObject(reqInfo);
        try {
            /**
             * 校验必填业务参数是否为空
             */
            checkParams(jsonObject, AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_DEVICEID,
                    AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_MUFFLERCONTROL_TYPE);
        } catch (MyBusiException e) {
            message.setMsg(AppConstant.REPONSE_CODE.BUSI_ERROR, AppConstant.REPONSE_MSG.SYS_PARAM_ERROR_MSG,
                    e.getMessage());
            return message;
        }
        String deviceId = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_DEVICEID);
        String signStr = "";
        String topic = deviceId+"/"+deviceId+"/set";
        String controlType = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_MUFFLERCONTROL_TYPE);
        if("1".equals(controlType)){
            signStr = "{\"Device\":\"mute\"}";
        }
        message.getReponseMessage(signStr,topic,deviceId,cacheManager,message);
        return message;
    }

}
